package com.gx.controller;

import com.gx.aspect.annotation.OperationLogger;
import com.gx.core.shiro.utils.ShiroUtils;
import com.gx.domain.SysUser;
import com.gx.page.TableDataInfo;
import com.gx.response.R;
import com.gx.service.api.SysUserService;
import com.gx.utils.StringUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/users")
public class UserController extends BaseController {
    @Autowired
    private SysUserService sysUserService;

    @GetMapping
    public R list() {
        startPage();
        List<SysUser> sysUsers = sysUserService.queryAll();
        TableDataInfo dataTable = getDataTable(sysUsers);
        return R.ok().put("rows", dataTable.getRows()).put("total", dataTable.getTotal());
    }

    @OperationLogger(option = "修改用户")
    @PostMapping
    public R add(@RequestBody SysUser sysUser) {
        SysUser checkUser = sysUserService.queryObject(sysUser.getUserId());

        if (checkUser != null) {
            return R.error("用户已存在，请勿重复添加");
        }

        sysUserService.save(sysUser);
        return R.ok("新增成功");
    }

    @OperationLogger(option = "更新用户")
    @PutMapping
    public R update(@RequestBody SysUser sysUser) {
        sysUserService.updateBySelective(sysUser);
        return R.ok("更新成功");
    }

    @GetMapping("/updateCache")
    public R updateUserCache(String sysUserId) {
        sysUserService.updateCurrentUserCache(sysUserId);
        return R.ok("当前用户缓存更新成功");
    }

    @OperationLogger(option = "更改用户密码")
    @PatchMapping("/repassword")
    public R resetPassword(@RequestParam("newPassword") String newPwd, @RequestParam("oldPassword") String oldPwd) {
        String userId = ShiroUtils.getCurrentUser().getUserId();

        if (StringUtils.isEmpty(newPwd) || StringUtils.isEmpty("oldPwd") || StringUtils.isEmpty(userId)) {
            return R.error("获取数据失败，修改失败");
        }


        SysUser sysUser = sysUserService.queryObject(userId);
        String oldpwd = DigestUtils.sha256Hex(oldPwd);
        if (!sysUser.getPassword().equals(oldpwd)) {
            return R.error("密码不正确");
        }

        String newpwd = DigestUtils.sha256Hex(newPwd);
        if (StringUtils.equals(newpwd, sysUser.getPassword())) {
            return R.error("新密码不能与旧密码相同");
        }

        SysUser changeUser = new SysUser();
        changeUser.setPassword(newpwd);
        sysUserService.updatePassword(userId, changeUser);
        return R.ok("密码修改成功");
    }

    @OperationLogger(option = "用户删除")
    @DeleteMapping("/batch")
    public R batchDelete(@RequestParam("ids") String ids) {
        List<Long> collect = Arrays.stream(ids.split(",")).mapToLong(id -> Long.parseLong(id)).boxed().collect(Collectors.toList());
        sysUserService.batchDelete(collect);
        return R.ok("删除成功");
    }
}
